package club.zhilin.guli.service.cms.controller.backend;


import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.cms.domain.AdType;
import club.zhilin.guli.service.cms.service.IAdTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 推荐位 前端控制器
 * </p>
 *
 * @author zhilin
 * @since 2022-02-22 16:00:09
 */
@RestController
@Api("推荐位管理")
@RequestMapping("/admin/cms/adType")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AdTypeController {
    private final IAdTypeService adTypeService;

    @ApiOperation("所有推荐类别列表")
    @GetMapping("/list")
    public CommonResult<List<AdType>> listAll() {
        List<AdType> list = adTypeService.list();
        return CommonResult.success(list);
    }

    @ApiOperation("推荐类别分页列表")
    @GetMapping("/list/{page}/{limit}")
    public CommonResult<Map<String, Object>> listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                                                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit) {
        Page<AdType> pageParam = new Page<>(page, limit);
        IPage<AdType> pageModel = adTypeService.page(pageParam);
        List<AdType> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        Map<String, Object> result = new HashMap<>(2);
        result.put("total", total);
        result.put("rows", records);
        return CommonResult.success(result);
    }

    @ApiOperation(value = "根据ID删除推荐类别")
    @DeleteMapping("/remove/{id}")
    public CommonResult<String> removeById(@ApiParam(value = "推荐类别ID", required = true) @PathVariable String id) {
        boolean result = adTypeService.removeById(id);
        return result ? CommonResult.success("删除成功") : CommonResult.failed("数据不存在");
    }

    @ApiOperation("新增推荐类别")
    @PostMapping("/save")
    public CommonResult<String> save(@ApiParam(value = "推荐类别对象", required = true) @RequestBody AdType adType) {
        boolean result = adTypeService.save(adType);
        return result ? CommonResult.success("保存成功") : CommonResult.failed("保存失败");
    }

    @ApiOperation("更新推荐类别")
    @PutMapping("/update")
    public CommonResult<String> updateById(@ApiParam(value = "讲师推荐类别", required = true) @RequestBody AdType adType) {
        boolean result = adTypeService.updateById(adType);
        return result ? CommonResult.success("修改成功") : CommonResult.failed("修改失败");
    }

    @ApiOperation("根据id获取推荐类别信息")
    @GetMapping("/get/{id}")
    public CommonResult<AdType> getById(@ApiParam(value = "推荐类别ID", required = true) @PathVariable String id) {
        AdType adType = adTypeService.getById(id);
        return adType != null ? CommonResult.success(adType) : CommonResult.failed("数据不存在");
    }
}

